# This file calculates year specific distances


rm(list=ls(all=TRUE))
gc()
library(Matrix)
library(foreign)
library(MASS)
library(matrixStats)
rxOptions(sampleDataDir = system.file("SampleData", package="RevoScaleR"),
          demoScriptsDir = system.file("demoScripts", package="RevoScaleR"),
          blocksPerRead = 1,
          reportProgress = 3,
          rowDisplayMax = -1,
          memStatsReset = 0,
          memStatsDiff = 0,
          numCoresToUse = 16,
          showTransformFn = FALSE)
setMKLthreads(8) 
setwd("D:/Dropbox/Spillovers/prog")


gc()

Distance.All.Combined<- function(i) {
gc()

file <-	paste("mergerData_",i,".dta", sep="")
file.forward <-	paste("mergerData_forwardCites_",i,".dta", sep="")
file.backward <-	paste("mergerData_backwardCites_",i,".dta", sep="")
file.weight<-paste("mergeCitesWeight_",i,".dta", sep="")
file.malhanobis<-paste("malhanobisWeight",i,".dta", sep="")
file.funding<-paste("funding_",i,".dta", sep="")
file.patent<-paste("patent_",i,".dta", sep="")

fileOut <-	paste("CitesWeightOut_",i,".dta", sep="")
fileOut.Distance <-	paste("Distance_",i,".dta", sep="")
fileOut.Distance.long <-	paste("DistanceLong_",i,".dta", sep="")
fileOut.Distance.Counterfactual <-	paste("DistanceC_",i,".dta", sep="")
fileOut.Distance.Counterfactual1 <-	paste("Distance_RFILE.dta", sep="")
forwardCites<-read.dta(file=file.path(getwd(), file.forward))
backwardCites<-read.dta(file=file.path(getwd(), file.backward))

weight<-as.matrix(read.dta(file=file.path(getwd(), file.weight)))
funding<-as.matrix(read.dta(file=file.path(getwd(), file.funding)))
patents<-as.matrix(read.dta(file=file.path(getwd(), file.patent)))

patentNumbers<-read.dta(file=file.path(getwd(), file))
patentNumbers<-as.matrix(patentNumbers[, 3:(ncol(patentNumbers))]) # Shares on the columns, companies on the rows
patentNumbers.colSum <- as.matrix(colSums(patentNumbers))
patentNumbers.shares<-sweep(patentNumbers, MARGIN=1, rowSums(patentNumbers), "/")
patentNumbers.shares[is.na(patentNumbers.shares)]<-0


weighing<-as.matrix(weight[, 2:(ncol(weight))]) # To nclasses on the rows and from nclasses on the columns 
weighing.rowShare<-as.matrix(sweep( weighing,MARGIN=1,rowSums(weighing),"/"))
weighing<-weighing.rowShare
weighing[is.na(weighing)]<-0


## Create Mahalanobis measure
# Weighing Matrix
weights.Mal<-crossprod(patentNumbers.shares)
diagonal.Malhanobis<-as.matrix(diag(weights.Mal), 1,0)
diagonal.Malhanobis <- (sweep( matrix(1,NROW(weights.Mal), NROW(weights.Mal)),MARGIN=2,diagonal.Malhanobis,"*"))^0.5
diagonal.Malhanobis<-t(diagonal.Malhanobis)*diagonal.Malhanobis
weights.Mal<-weights.Mal/(diagonal.Malhanobis+1)

# Standardize matrix (for all types)
basestandard<-tcrossprod(patentNumbers.shares)
diagonal<-(as.matrix(diag(basestandard), 1,0))^0.5
share<-patentNumbers.shares
shares <- (sweep(patentNumbers.shares,MARGIN=1,diagonal,"/"))
shares[is.na(shares)]<-0

Jaffe.Base<-as.matrix(tcrossprod(shares, shares))
diag(Jaffe.Base) <- 0 # No own spillovers
Jaffe.Malhanobis<-tcrossprod(tcrossprod(shares,weights.Mal), shares)
diag(Jaffe.Malhanobis) <- 0
Jaffe.Cites<-tcrossprod(tcrossprod(shares, weighing),shares ) # From ist auf der x-Achse und to ist auf der y-achse
diag(Jaffe.Cites) <- 0 



# Calculate weights for Jaffe Mal Cites

Jaffe.Base.colSums.Venture<-as.matrix(colSums(Jaffe.Base[1:colSums(funding[, 1:2]>0)[2], ] )) # Hope I get this right. Should just sum over venture
Jaffe.Cites.colSums.Venture<-as.matrix(colSums(Jaffe.Cites[1:colSums(funding[, 1:2]>0)[2], ]))
Jaffe.Malhanobis.colSum.Venture<-as.matrix(colSums(Jaffe.Malhanobis[1:colSums(funding[, 1:2]>0)[2], ]))

Jaffe.Base.colSums<-as.matrix(colSums(Jaffe.Base))-Jaffe.Base.colSums.Venture
Jaffe.Cites.colSums<-as.matrix(colSums(Jaffe.Cites))-Jaffe.Cites.colSums.Venture
Jaffe.Malhanobis.colSums<-as.matrix(colSums(Jaffe.Malhanobis))-Jaffe.Malhanobis.colSum.Venture

Distance<-as.data.frame(cbind(funding[, 1:2], Jaffe.Base.colSums.Venture, Jaffe.Base.colSums, Jaffe.Cites.colSums.Venture, Jaffe.Cites.colSums, Jaffe.Malhanobis.colSum.Venture, Jaffe.Malhanobis.colSums))
#rm(Jaffe.Base.colMeans, Jaffe.Base.rowMeans, Jaffe.Cites.colMeans, Jaffe.Cites.rowMeans, Jaffe.Malhanobis.colMeans, Jaffe.Malhanobis.rowMeans)
names(Distance)<-c("gvkey", "ID", "weightVentureJaffe","weightEstJaffe", "weightVentureCites", "weightEstCites","weightVentureMal", "weightEstMal")
write.dta(Distance, file=file.path(getwd(), fileOut.Distance.Counterfactual))
if (i==2000) {
# Average Distances for summary statistics
Jaffe.Base.colMeans<-as.matrix(colMeans(Jaffe.Base)) # Hope I get this right. Should just sum over venture
Jaffe.Cites.colMeans<-as.matrix(colMeans(Jaffe.Cites))
Jaffe.Malhanobis.colMeans<-as.matrix(colMeans(Jaffe.Malhanobis))

Distance<-as.data.frame(cbind(funding[, 1:2], Jaffe.Base.colMeans, Jaffe.Cites.colMeans,Jaffe.Malhanobis.colMeans))

names(Distance)<-c("gvkey", "ID", "weightJaffe", "weightCites", "weightMal")
write.dta(Distance, file=file.path(getwd(), fileOut.Distance.Counterfactual1))	


Jaffe.Base.vector<-as.matrix(as.vector(Jaffe.Base))
Jaffe.Cites.vector<-as.matrix(as.vector(Jaffe.Cites))
Jaffe.Cites.vectorTranspose<-as.matrix(as.vector(t(Jaffe.Cites)))
Jaffe.Malhanobis.vector<-as.matrix(as.vector(Jaffe.Malhanobis))
Distance<-as.data.frame(cbind(Jaffe.Base.vector, Jaffe.Cites.vector,Jaffe.Malhanobis.vector, Jaffe.Cites.vectorTranspose))
names(Distance)<-c("Jaffe", "Cites", "Mal", "Cites2")
write.dta(Distance, file=file.path(getwd(), fileOut.Distance.long))
rm(Jaffe.Base.vector, Jaffe.Cites.vector, Jaffe.Malhanobis.vector, Jaffe.Cites.vectorTranspose)

}

lenght=NROW(Jaffe.Base)*NCOL(Jaffe.Base)
breite = ncol(funding)-3
Jaffe.spillover<-crossprod(Jaffe.Base, funding[,3:NCOL(funding)]) #Is that the right distance funding combination
Jaffe<-cbind(funding[, 1:2], Jaffe.spillover)
Jaffe.Cites.spillover<-crossprod(Jaffe.Cites, funding[,3:NCOL(funding)]) #Ich will From auf der x-Achse haben weil es nochmal transponiert wird im crossprod
Jaffe.Malhanobis.spillover<-crossprod(Jaffe.Malhanobis, funding[,3:NCOL(funding)]) #Is that the right distance funding combination


Jaffe<-as.data.frame(cbind(funding[, 1:2], Jaffe.spillover, Jaffe.Cites.spillover, Jaffe.Malhanobis.spillover))

rm(Jaffe.Base, Jaffe.Cites, Jaffe.Malhanobis)

nameVector = c("J_", "C_", "M_")
temp<-colnames(funding[,3:NCOL(funding)])
names<-c("gvkey", "ID")
for (j in 1:3) {
for (i in 1:NCOL(funding[,3:NCOL(funding)])) {
names.temp<-paste(nameVector[j],temp[i], sep = "")
names <- c(names, names.temp)
}
}


names(Jaffe)<-names
gc()
write.dta(Jaffe, file=file.path(getwd(), fileOut))


}
ptm <- proc.time()
for (i in 1979:2005){

Distance.All.Combined(i)

}
proc.time() - ptm


